﻿namespace Framework
{

    public class UIFormInstanceObject : ObjectBase
    {
        private object m_UIFormAsset;
        
        private IUIFormHelper m_UIFormHelper;

        public UIFormInstanceObject()
        {
            m_UIFormAsset = null;
            m_UIFormHelper = null;
        }

        /// <summary>
        /// 创建实例
        /// </summary>
        /// <param name="name"></param>
        /// <param name="uiFormAsset"></param>
        /// <param name="uiFormHelper"></param>
        /// <returns></returns>
        /// <exception cref="GameFrameworkException"></exception>
        public static UIFormInstanceObject Create(string name, object uiFormAsset,UIFormHelper  uiFormHelper)
        {
            if (uiFormAsset == null)
            {
                throw new GameFrameworkException("UI form asset is invalid.");
            }

            if (uiFormHelper == null)
            {
                throw new GameFrameworkException("UI form helper is invalid.");
            }
            UIFormInstanceObject uiFormInstanceObject = ReferencePool.Acquire<UIFormInstanceObject>();
            uiFormInstanceObject.m_UIFormHelper = uiFormHelper;
            uiFormInstanceObject.m_UIFormAsset = uiFormAsset;
            uiFormInstanceObject.Init(name);
            return uiFormInstanceObject;
        }


        protected  void Init(string name)
        {
            object uiFormInstance = m_UIFormHelper.InstantiateUIForm(m_UIFormAsset);
            base.Init(name,uiFormInstance);
        } 

        public override void Clear()
        {
            base.Clear();
            m_UIFormAsset = null;
            m_UIFormHelper = null;
        }

        protected internal override void Release(bool isShutdown)
        {
            m_UIFormHelper.ReleaseUIForm(m_UIFormAsset, Target);
        }
    }

}